import * as bm from "/js/baseModule.js";

require('/js/collapse.js');

export class Module extends bm.baseModule {
    constructor(view) {
        super(view);
        let that = this;
        this.proInfo = {};
        this.int1 = '';
        this.int2 = '';
        this.int3 = '';
        this.pointAtLeast = '';
        this.scope = this;
        this.isBomb = false;
        this.TOTAL_GRAB_NUMBER = 1;

        // 如果id不存在
        if (globalData.current_demand.id == '') {
            // 如果浏览器链接有did参数，则刷新
            if (funs.qs('did')) {
                window.location.reload();
            } else {
                location.hash = 'index';
            }
        }
        //详情页渲染
        let v_details = this.find('.DemandInfo_vue')[0];
        let data = globalData;
        this.proInfo = new Vue({
            el: v_details,
            data: data,
            methods: {
                updateTime: function (startTime) {
                    startTime = startTime + '';
                    let text;
                    let stime = startTime * 1000;
                    let date = new Date().getTime();
                    let gapMin = Math.floor((date - stime) / 1000 / 60);
                    if (gapMin <= 0) {
                        //防止偶尔出现的js取时间不准确
                        gapMin = 1;
                    }
                    let gapHour = Math.floor((date - stime) / 1000 / 60 / 60);

                    if (gapHour <= 24) {
                        text = gapHour + '小时前';
                        if (gapMin < 60) {
                            text = gapMin + '分钟前';
                        }
                    } else if (gapHour <= 24 * 8) {
                        gapHour = Math.floor(gapHour / 24);
                        text = gapHour + '天前'
                    } else if (gapHour > 24 * 8) {
                        let ddd = new Date(startTime * 1000);
                        gapHour = (ddd.getMonth() + 1) + '-' + ddd.getDate();
                        text = gapHour
                    } else {
                        //防止意外情况，暂时如此处理
                        text = '刚刚'
                    }
                    return text;
                },
                /*搜索词*/
                sem: function (str) {
                    if (str) {
                        str = str.replace(/\'/g, '"');
                        let sem = JSON.parse(str);
                        return sem.word;
                    } else {
                        return "";
                    }
                },
                /*input修改时，隐藏错误提示*/
                inputFunc: function () {
                    that.find(".errorTip").hide();
                    let val = that.find('.offer-input').val();
                    if (parseInt(val) >= 200) {
                        //爆灯
                        that.find('.new-offer-input').val(200);
                    }
                    this.current_demand.curr_offer = that.find('.new-offer-input').val();

                },
            },

            computed: {
                // 竞价的工程上列表处理
                sList: function () {
                    let list = this.current_demand.supplier_list;
                    let status = this.current_demand.status;
                    if (list.length == 0) {
                        return []
                    } else {
                        // 旧订单特殊处理
                        let now = new Date().getTime();
                        if (this.current_demand.published_at < 1509968204) {
                            for (let i = 0; i < list.length; i++) {
                                list[i].succ_item = false;
                                list[i].info = '通话结束';
                                list[i].oldOrder = true;
                            }
                        } else {
                            //首轮竞价
                            if (status == '10') {
                                let len = (list.length > this.TOTAL_GRAB_NUMBER) ? this.TOTAL_GRAB_NUMBER : list.length;
                                for (let j = 0; j < len; j++) {
                                    list[j].succ_item = true;
                                    list[j].info = '暂时领先';
                                }
                                if (list[0].offer == 200) {
                                    list[0].info = '成就值较高,暂时领先';

                                }

                                // if (status == '15' || '20' || '30' || '100' || '200)
                            } else {
                                for (let i = 0; i < list.length; i++) {
                                    list[i].oldOrder = false;
                                    if (list[i].status == '130') {
                                        list[i].succ_item = false;
                                        list[i].info = '超时未联系';
                                        continue
                                    }
                                    if (list[i].status == '110' || list[i].status == '120') {
                                        list[i].succ_item = true;
                                        list[i].info = '待呼叫';
                                        // if (i == 0) {
                                        //     list[0].info = '正在呼叫客户';
                                        // }
                                        continue
                                    }
                                    if (list[i].status == '150') {
                                        list[i].succ_item = true;
                                        list[i].info = '正在呼叫客户';
                                        continue
                                    }
                                    if (list[i].status == '200') {
                                        list[i].succ_item = true;
                                        list[i].info = '通话结束' + that.transTime(list[i].contact_seconds);
                                        if (list[i].contact_seconds <= 30) {
                                            list[i].info = '通话结束'
                                        }
                                        // // 机器人随机显示60s-180s的任意一个时间，
                                        // if (list[i].is_robot) {
                                        //     var second = parseInt(Math.random() * (180 - 60 + 1) + 60, 10);
                                        //     list[i].info = '通话结束' + that.transTime(second);
                                        // }

                                        if (this.current_demand.for_gold) {
                                            list[i].info = '竞价成功';
                                        }
                                        continue
                                    }
                                    if (list[i].status == '10') {
                                        list[i].succ_item = false;
                                        list[i].info = '拟出局';
                                        if (status == '100' || status == '200') {
                                            list[i].info = '出局';
                                        }
                                    }
                                }
                            }
                        }
                        return list
                    }
                },
                btnStatus: function () {
                    let status = this.current_demand.status;
                    let partic = this.current_demand.supplier_partic_status;
                    let userLevel = this.user.level;
                    let userId = this.user.user_id;
                    let tag = this.current_demand.tag;
                    let has_max = this.current_demand.has_max;
                    let self_has_max = this.current_demand.self_has_max;
                    let curr_offer = this.current_demand.curr_offer;//当前输入框的值
                    let particList = this.current_demand.supplier_list || [];//当前参与列表
                    let currPartic = [];//当前参与列表
                    let index = -1;//我的名次
                    let price = this.current_demand.price; // 一口价的价格
                    let expected_bid_point = this.current_demand.expected_bid_point; // 铜牌一口价的价格


                    let btnText='', evt='';
                    //循环检测自己当前名次
                    $.each(particList, function (i, item) {
                        if (item.id == userId) {
                            currPartic = particList[i];
                            index = i;
                        }
                    })
                    // 完成的项目
                    if (status == 200) {
                        // if (partic == 200 && status == 100) {
                        //     btnText = '<span class="icon-phone06"></span>' + this.current_demand.user.phone;
                        //     evt = 'fromWPH';
                        // } else {
                        btnText = '去看看其他订单';
                        evt = 'goIndex';
                        // }
                    } else {
                        if (tag == '1') {
                            btnText = '已被其他金牌锁定';
                            evt = 'goIndex';
                        } else if (tag == '2') {
                            btnText = '已到达人数限制';
                            evt = 'goIndex';
                        } else if (tag == '110') {
                            btnText = '<span class="icon-phone06"></span>' + this.current_demand.user.phone;
                            evt = 'fromWPH';
                        } else if (tag == '0') {
                            //铜牌一口价中
                            if (status == '8') {
                                //是铜牌才可以抢
                                if (userLevel == '20') {
                                    //未参与，可报价
                                    if (partic == '0') {
                                        btnText = '<span class="eight-money">' + expected_bid_point + '汇币</span> <span class="eight">(历史同类订单价8折)</span>';
                                        evt = 'grab';
                                    } else if (partic == '10') {
                                        // btnText = '排到时，将微信通知您';
                                        //南向说，把所有的 排到时，将微信通知您 改成点点点的loading......
                                        btnText = '<img src="/img/demand/loading.gif" style="width: .8rem;"/>'
                                        evt = '';
                                    } else if (partic == '110') {
                                        btnText = '<span class="icon-phone06"></span>' + this.current_demand.user.phone;
                                        evt = 'fromWPH';
                                    } else if (partic == '120') {
                                        let point = parseInt(this.current_demand.my_offer) - parseInt(this.pack.call_point);
                                        btnText = '余额不足，需缴纳' + point + '汇币';
                                        evt = 'loadValueAddService';
                                    } else if (partic == '130') {
                                        btnText = '超时未联系';
                                        evt = '';
                                    } else if (partic == '150') {
                                        btnText = '<span class="icon-phone06"></span>' + this.current_demand.user.phone;
                                        evt = 'fromWPH';
                                    } else if (partic == '200') {
                                        btnText = '<span class="icon-phone06"></span>' + this.current_demand.user.phone;
                                        evt = 'fromWPH';
                                    }
                                } else {
                                    //原则上应该返回项目大厅
                                    /*
                                    
                                    */
                                    location.hash = 'index';
                                    return;
                                }
                            } else if (status == '10') {
                                // 一轮竞价中
                                if (self_has_max) {
                                    //我已经爆过灯(参与过)
                                    btnText = '您已爆灯，请等待...';
                                    evt = '';
                                } else if ((partic == '0' || partic == '10') && (has_max || curr_offer == 200)) {
                                    //有人爆灯 || 我自己的输入框值 == 200
                                    btnText = '<span class="icon-baodeng"></span>我要爆灯';
                                    evt = 'bomb';
                                } else if (partic == '10') {
                                    //参与过
                                    btnText = '再次出价';
                                    evt = 'NewGrab';
                                } else if (partic == '110') {
                                    btnText = '<span class="icon-phone06"></span>' + this.current_demand.user.phone;
                                    evt = 'fromWPH';

                                } else if (partic == '120') {
                                    let point = parseInt(this.current_demand.my_offer);
                                    btnText = '余额不足，需缴纳' + point + '汇币';
                                    evt = 'loadValueAddService';
                                } else if (partic == '130') {
                                    btnText = '超时未联系';
                                    evt = '';
                                } else if (partic == '150') {
                                    btnText = '<span class="icon-phone06"></span>' + this.current_demand.user.phone;
                                    evt = 'fromWPH';

                                } else if (partic == '200') {
                                    btnText = '<span class="icon-phone06"></span>' + this.current_demand.user.phone;
                                    evt = 'fromWPH';
                                } else {
                                    btnText = '我要出价';
                                    evt = 'NewGrab';
                                }
                                //竞价结束前的中间态
                            } else if (status == '11') {

                                // 一轮结束
                            } else if (status == '15') {
                                //竞价结束---目前看不会有这个状态存在
                                //未参与
                                if (partic == '0') {
                                    btnText = '去看看其他订单';
                                    evt = 'goIndex';

                                    // 排队中
                                } else if (partic == '10') {
                                    // btnText = '排到时，将微信通知您';
                                    btnText = '<img src="/img/demand/loading.gif" style="width: .8rem;"/>'
                                    evt = '';
                                } else if (partic == '110') {
                                    btnText = '<span class="icon-phone06"></span>' + this.current_demand.user.phone;
                                    evt = 'fromWPH';

                                    // btnText = '<span class="grab-succ">竞拍成功</span><span class="grab-small-font">约' + 5 * (this.TOTAL_GRAB_NUMBER -1) + '分钟为您接通客户，注意接听0216电话</span>';
                                    // btnText = '排到时，将电话通知您';
                                    // evt = '';
                                    // if (index == 0) {//如果我是第一名
                                    //     btnText = '正在呼叫客户，请稍候';
                                    //     evt = 'garbCalling';
                                    // }
                                } else if (partic == '120') {
                                    let point = parseInt(this.current_demand.my_offer);
                                    btnText = '余额不足，需缴纳' + point + '汇币';
                                    evt = 'loadValueAddService';
                                } else if (partic == '130') {
                                    btnText = '超时未联系';
                                    evt = '';
                                    // btnText = '去看看其他订单';
                                    // evt = 'goIndex';
                                } else if (partic == '150') {
                                    btnText = '<span class="icon-phone06"></span>' + this.current_demand.user.phone;
                                    evt = 'fromWPH';

                                    // btnText = '正在呼叫客户，请稍候';
                                    // evt = 'garbCalling';
                                } else if (partic == '200') {
                                    btnText = '<span class="icon-phone06"></span>' + this.current_demand.user.phone;
                                    evt = 'fromWPH';
                                }

                                //  二/三 次竞拍中
                            } else if (status == '20' || status == '30') {
                                //未参与，可报价
                                if (partic == '0' && status == '20') {
                                    btnText = '我要出价 <span class="money">(' + price + '汇币)</span>';
                                    evt = 'grab';

                                } else if (partic == '0' && status == '30') {
                                    btnText = '免费抢单';
                                    evt = 'grab';

                                } else if (partic == '10') {
                                    // btnText = '排到时，将微信通知您';
                                    btnText = '<img src="/img/demand/loading.gif" style="width: .8rem;"/>'
                                    evt = '';
                                } else if (partic == '110') {
                                    btnText = '<span class="icon-phone06"></span>' + this.current_demand.user.phone;
                                    evt = 'fromWPH';
                                    // btnText = '<span class="grab-succ">竞拍成功</span><span class="grab-small-font">约' + 5 * (this.TOTAL_GRAB_NUMBER -1) + '分钟为您接通客户，注意接听0216电话</span>';
                                    // btnText = '排到时，将电话通知您';
                                    // evt = '';
                                } else if (partic == '120') {
                                    let point = parseInt(this.current_demand.my_offer) - parseInt(this.pack.call_point);
                                    btnText = '余额不足，需缴纳' + point + '汇币';
                                    evt = 'loadValueAddService';
                                } else if (partic == '130' && status == '20') {
                                    btnText = '超时未联系';
                                    evt = '';
                                    // btnText = '我要出价 <span class="money">(' + price + '汇币)</span>';
                                    // evt = 'grab';
                                } else if (partic == '130' && status == '30') {
                                    btnText = '超时未联系';
                                    evt = '';
                                    // btnText = '免费抢单';
                                    // evt = 'grab';
                                } else if (partic == '150') {
                                    btnText = '<span class="icon-phone06"></span>' + this.current_demand.user.phone;
                                    evt = 'fromWPH';
                                    // btnText = '正在呼叫客户，请稍候';
                                    // evt = 'garbCalling';
                                } else if (partic == '200') {
                                    btnText = '<span class="icon-phone06"></span>' + this.current_demand.user.phone;
                                    evt = 'fromWPH';
                                }
                            } else if (status == '100') {
                                //抢单结束或完成的项目
                                //未参与
                                if (partic == '0') {
                                    btnText = '去看看其他订单';
                                    evt = 'goIndex';
                                    // 排队中
                                } else if (partic == '10') {
                                    btnText = '去看看其他订单';
                                    evt = 'goIndex';
                                } else if (partic == '110') {
                                    btnText = '<span class="icon-phone06"></span>' + this.current_demand.user.phone;
                                    evt = 'fromWPH';
                                    // btnText = '<span class="grab-succ">竞拍成功</span><span class="grab-small-font">约' + 5 * (this.TOTAL_GRAB_NUMBER -1) + '分钟为您接通客户，注意接听0216电话</span>';
                                    // btnText = '排到时，将电话通知您';
                                    // evt = '';
                                    // if (index == 0) {//如果我是第一名
                                    //     btnText = '正在呼叫客户，请稍候';
                                    //     evt = 'garbCalling';
                                    // }
                                } else if (partic == '120') {
                                    let point = parseInt(this.current_demand.my_offer);
                                    btnText = '余额不足，需缴纳' + point + '汇币';
                                    evt = 'loadValueAddService';
                                } else if (partic == '130') {
                                    btnText = '超时未联系';
                                    evt = '';
                                    // btnText = '去看看其他订单';
                                    // evt = 'goIndex';
                                } else if (partic == '150') {
                                    btnText = '<span class="icon-phone06"></span>' + this.current_demand.user.phone;
                                    evt = 'fromWPH';
                                    // btnText = '正在呼叫客户，请稍候';
                                    // evt = 'garbCalling';
                                } else if (partic == '200') {
                                    btnText = '<span class="icon-phone06"></span>' + this.current_demand.user.phone;
                                    evt = 'fromWPH';
                                }

                            }
                        }
                    }
                    let btn = {
                        text: btnText,
                        evt: evt,
                    };
                    return btn
                },
                //申报审核结果
                finish_info: function () {
                    let info, btn, evt = '';
                    let status = this.current_demand.user_finish_img_info.state;
                    switch (status) {
                        case -1:
                            info = '项目已完成？申报可提升等级';
                            btn = '申报';
                            evt = 'loadDeclareProject';
                            break;
                        case 0:
                            info = '已申报，结果将微信通知您';
                            btn = '审核中';
                            break;
                        case 1:
                            info = '恭喜，你的申报已审核通过';
                            btn = '审核通过';
                            break;
                        case 2:
                            info = '抱歉，你的申报审核失败';
                            btn = '审核失败';
                            break;
                    }
                    return { info: info, btn: btn, evt: evt }
                },
                //申报审核结果
                feedback_info: function () {
                    let info, btn, evt = '';
                    let status = this.current_demand.feedback_info.feedback_status;
                    let my_follow_type = this.current_demand.my_follow_type;
                    switch (status) {
                        case 0:
                            info = '信息有误？反馈成功返还汇币';
                            btn = '反馈';
                            evt = 'openFeedbackDemand';
                            break;
                        case 3:
                            info = '已反馈，结果将微信通知您';
                            btn = '审核中';
                            break;
                        case 2:
                            info = '恭喜，你的反馈已审核通过';
                            btn = '审核通过';
                            if (my_follow_type == 1) {
                                btn = '已锁定';
                            }
                            break;
                        case 1:
                            info = '抱歉，你的反馈信息审核失败';
                            btn = '已驳回';
                            break;
                    }
                    if (this.current_demand.status == 200) {
                        evt = '';
                    }
                    return { info: info, btn: btn, evt: evt }
                },
                //第N次发布理由列表处理
                publish_remark_list: function () {
                    let count = this.current_demand.published_count;
                    let remark = JSON.parse(this.current_demand.publish_remark);
                    let list = new Array();
                    let aaa;
                    if (!remark) {
                        return
                    }
                    for (let i = 1; i <= count; i++) {
                        if (remark[i + '']) {
                            aaa = { 'inedx': i, 'val': remark[i + ''] };
                            list.push(aaa);
                        }
                    }
                    return list.reverse();
                }
            },
            watch: {
                "current_demand.status": function (val, oldV) {
                    if (oldV != val) {
                        that.countOfferPoint();
                        api.getDemandInfo({ "id": this.current_demand.id }, {
                            succ: function (demand) {
                                $.extend(true, globalData.current_demand, demand);
                                if ((oldV == 10 || oldV == 11) && val == 15) {
                                    that.grabCount();

                                    // 竞价结束，但是当前用户仍是排队状态，则弹出”拟出局“
                                    if (globalData.current_demand.supplier_partic_status == '10') {
                                        funs.openModal('modules/modals/CommonModal.JS', function (mod) {
                                            mod.setType(4);
                                        })
                                    }
                                }
                                /*铜牌一口价变成竞价模式,当前用户是铜牌,有铜牌抢 但是当前用户仍是排队状态，或未参与，则弹出”被其他铜牌抢走“*/
                                if (oldV == 8 && val == 10) {
                                    that.grabCount();

                                    if (globalData.user.level == 20 && globalData.current_demand.bronze_partic_info.id != 0 && (globalData.current_demand.supplier_partic_status == '0' || globalData.current_demand.supplier_partic_status == '10')) {
                                        funs.openModal('modules/modals/CommonModal.JS', function (mod) {
                                            mod.setType(21, { phone: globalData.current_demand.bronze_partic_info.mobile });
                                        })
                                    }
                                }
                                /*被锁定 在铜牌一口价或者竞价中的时候，订单被锁定，当前用户也没有参与竞价，提示被锁定*/
                                if ((oldV == 8 || oldV == 10) && val == 100 && globalData.current_demand.has_demand_locked && (globalData.current_demand.supplier_partic_status == '0' || globalData.current_demand.supplier_partic_status == '10')) {

                                    if (globalData.current_demand.bronze_partic_info.mobile) {
                                        funs.tip("竞价失败", "已被铜牌<span class='text-primary'>" + globalData.current_demand.bronze_partic_info.mobile + "</span>锁定，铜牌真烦人！", "fail", 5000);
                                    }
                                }
                            }
                        });
                    }
                },//二次发布时，监听发布时间，改变倒计时
                "current_demand.published_at": function () {
                    that.grabCount();
                },
                "current_demand.supplier_partic_status": function (val, oldV) {
                    if (globalData.current_demand.partic_demand_status == '9') {
                        return
                    }
                    if (oldV != val) {
                        that.countOfferPoint();
                        api.getDemandInfo({ "id": globalData.current_demand.id }, {
                            succ: function (demand) {
                                $.extend(true, globalData.current_demand, demand);
                                if ((val == '110' || val == '120') && (oldV == 0 || oldV == '10')) {
                                    that.grabCount();
                                    // 打开竞价成功弹窗
                                    let number = Math.floor(globalData.current_demand.my_offer * 0.3);
                                    let str = number + '个(30%)';
                                    funs.openModal('modules/modals/CommonModal.JS', function (mod) {
                                        mod.setType(3, { minusCoin: str });
                                    });

                                }
                            }
                        });
                    }
                },
            }
        });

        /**
         * 如果是直接进入当前页面，则先loading一会儿，等数据回来
         */
        if (globalData.entryPage == 'DemandDetail') {
            this.find('.sendImgPending').removeClass('hide');
            let id = globalData.current_demand.id;
            api.getDemandInfo({ "id": id }, {
                succ: function (demand) {
                    $.extend(true, globalData.current_demand, demand);
                    that.find('.sendImgPending').addClass('hide');
                    that._init();
                },
                fail: function (json) {
                    //  如果是项目已被删除或者不存在
                    if (json.status = "41004002") {
                        funs.openModal('modules/modals/CommonModal.JS', function (mod) {
                            mod.setType(9);
                        })
                    } else {
                        funs.tip(json.msg, '', 'fail');
                        setTimeout(function () {
                            that._evt_goback();
                        }, 2000)
                    }
                }
            });
        }

        /** 折叠 */
        $('.collapse').collapse();
        $('.panel-collapse').on('show.bs.collapse', function () {
            // 展开执行一些动作...
            let panelCollapse = $(this);
            panelCollapse.parent().find('.icon-circle-down').addClass('rotate');
            panelCollapse.parent().find('.panel-heading').css('padding-bottom', '0');

        }).on('hide.bs.collapse', function () {
            // 折叠执行一些动作...
            let panelCollapse = $(this);
            panelCollapse.parent().find('.icon-circle-down').removeClass('rotate');
            panelCollapse.parent().find('.panel-heading').css('padding-bottom', '0.1rem');
        }).on('shown.bs.collapse', function () {
            // 执行一些动作...
            $('#DemandDetail').scrollTop($('#DemandDetail')[0].scrollHeight);
        });

        //项目浏览数+1
        // api.demandAddScan({ "id": parseInt(globalData.current_demand.id) }, {
        //     succ: function () {
        //         console.log('项目浏览量+1');
        //         globalData.current_demand.scan_count += 1;
        //     }
        // });

    }

    _init() {
        let that = this;
        //汇币的计算
        this.countOfferPoint();

        /** 定时任务：定时请求项目参与表，并更新相关数据 */
        let demand = globalData.current_demand;
        // 进行中的不为采购的真实项目，才会更新数据
        if (demand.tag == 0 && demand.cate != '4' && (demand.status == '8' || demand.status == '10' || demand.status == '11' || demand.status == '15' || demand.status == '20' || demand.status == '30')) {
            // 获取项目参与列表,更新参与表和项目状态
            window.clearInterval(this.int2);
            this.int2 = setInterval(function () {
                api.getParticInfo({ demand_id: globalData.current_demand.id }, {
                    succ: function (json) {
                        let list = json.partic_supplier_list;
                        let isSame = true;
                        let a = JSON.stringify(globalData.current_demand.supplier_list);
                        let b = JSON.stringify(list);
                        a = a.replace(/\"info\":\"[\u4e00-\u9fa5]{3,6}\"/g, '').replace(/\"succ\_item\":[a-z]{4,5}/g, '');
                        b = b.replace(/\"info\":\"[\u4e00-\u9fa5]{3,6}\"/g, '').replace(/\"succ\_item\":[a-z]{4,5}/g, '');
                        if (a != b) isSame = false;
                        globalData.current_demand.supplier_list.length = 0;
                        globalData.current_demand.supplier_list.push(...list);
                        globalData.current_demand.status = json.demand_status;
                        globalData.current_demand.has_max = json.has_max;
                        //找到自己的信息并更新项目参与状态
                        $.each(list, function (index, value) {
                            if (value.id == globalData.user.user_id) {
                                globalData.current_demand.supplier_partic_status = value.status;
                            }
                        });
                        let bronze_partic_info = json.bronze_partic_info;
                        //铜牌一口价下成功信息
                        $.extend(true, globalData.current_demand.bronze_partic_info, bronze_partic_info);
                        //如果在一阶段，监控到有新报价，则更新现有报价
                        if (!isSame && json.demand_status == '10') {
                            that.countOfferPoint();
                        }
                    }
                });
            }, 2000);
        }
    }

    //页面加载事件
    afterInstall() {
        let that = this;
        $('.panel-collapse').collapse('hide');
        if (globalData.entryPage != 'DemandDetail') {
            this._init();
        }
        if (globalData.entryPage == 'DemandDetail') {
            api.getUserInfo({
                succ: function (json) {
                    $.extend(globalData.user, json.user);
                    $.extend(true, globalData.pack, json.pack);
                    globalData.pack.uid = json.user.user_id;
                    funs.removeWelcome();
                    that.grabCount();

                    // 如果是直接进入当前页面，且项目已被删除或者不存在，则回到项目大厅
                    if (globalData.entryPage == 'DemandDetail' && globalData.current_demand.uid == '0000X') {
                        //订单不可见
                        funs.openModal('modules/modals/CommonModal.JS', function (mod) {
                            mod.setType(9);
                        })
                    }
                }
            });
        }
        // 无论啥类型项目，都要去取参与列表
        api.getParticInfo({ demand_id: globalData.current_demand.id }, {
            succ: function (json) {
                let list = json.partic_supplier_list;
                let bronze_partic_info = json.bronze_partic_info;
                //铜牌一口价下成功信息
                $.extend(true, globalData.current_demand.bronze_partic_info, bronze_partic_info);
                globalData.current_demand.supplier_list.length = 0;
                globalData.current_demand.supplier_list.push(...list);
                globalData.current_demand.status = json.demand_status;
                //找到自己的信息并更新项目参与状态
                $.each(list, function (index, value) {
                    if (value.id == globalData.user.user_id) {
                        globalData.current_demand.supplier_partic_status = value.status;
                    }
                });

                //抢单倒计时
                that.grabCount();
                that.countOfferPoint();
            }
        });

    }

    /*==============录音音频处理-start=============*/

    //录音播放暂停
    _evt_playPause(target) {
        if (globalData.current_demand.supplier_partic_status != '200' && globalData.current_demand.audio_sensitive) {
            //audio_sensitive==true 敏感录音
            //未参与并且是敏感录音，弹一个提示气泡
            funs.openModal('modules/modals/CommonModal.JS', function (mod) {
                mod.setType(7);
            });
            return;
        }

        let that = this;
        if (globalData.current_demand.tag != 0) {
            //虚拟订单不可播放
            return;
        }
        //如果录音加载失败
        let audioTime = this.find('.audio-time');
        let audio = document.getElementById("audioTag");
        if (audio.paused) {
            audio.play();
            this.find('.audio-time').removeClass('hide');
            this.find('.txt').addClass('hide');
            //改变icon
            $(target).find('.play-pause>span').removeClass('icon-play-one').addClass('icon-pause-one')
        } else {
            audio.pause();
            $(target).find('.play-pause>span').removeClass('icon-pause-one').addClass('icon-play-one')
        }

        //监听音频播放时间并更新进度条
        audio.addEventListener('timeupdate', function () {
            //更新进度条
            let audio = document.getElementsByTagName('audio')[0];
            // $('.audio-time').html(that.transTime(audio.duration - audio.currentTime));
            let rate = parseInt(audio.currentTime / audio.duration * 100 * 3.6);
            if (rate <= 180) {
                $('.circle_right').css('transform', 'rotate(' + rate + 'deg)');
            } else {
                $('.circle_right').css({
                    'transform': 'rotate(0deg)',
                    "background": "#f8d498"
                });
                $('.circle_left').css('transform', 'rotate(' + (rate - 180) + 'deg)');
            }
        }, false);
        //监听播放完成事件
        audio.addEventListener('ended', function () {
            //播放完成
            let audio = document.getElementsByTagName('audio')[0];
            audio.currentTime = 0;
            audio.pause();
            that.find('.play-pause>span').removeClass('icon-pause-one').addClass('icon-play-one');
            that.find('.audio-time').addClass('hide');
            that.find('.txt').removeClass('hide');
            $('.circle_right').css({
                'transform': 'rotate(0deg)',
                "background": "#FFECCD"
            });
            $('.circle_left').css('transform', 'rotate(0deg)');
        }, false);
    };

    //转换音频时长格式
    transTime(time) {
        let duration = parseInt(time);
        let minute = Math.floor(duration / 60);
        let sec = duration % 60 + '';
        let isM0 = ':';
        if (minute == 0) {
            minute = '00';
        } else if (minute < 10) {
            minute = '0' + minute;
        }
        if (sec.length == 1) {
            sec = '0' + sec;
        }
        return minute + isM0 + sec
    }

    /*==============录音音频处理-end=============*/

    //点击查看大图
    _evt_scanBig(obj) {
        let src = $(obj).attr('src');
        let width = $(obj).width();
        let height = $(obj).height();
        let rate = width / height;//宽高比
        let hh = $(window).width();//大图的宽 == 屏幕宽度
        let bgh = hh / rate / 2;  // 计算出大图的高度 de 一半

        //dom插入
        let bigImg = '<div class="bigImgBg" evt="closeBigImg"><img class="bigImg" src="' + src + '"/></div>';
        $(this.view).append(bigImg);

        //让图片垂直居中
        $('.bigImg').css('margin-top', -bgh + 'px');
    };

    _evt_closeBigImg(obj) {
        $(obj).remove();
    };

    /*================抢单逻辑－start===============
    * 当出价高于200时，最高为200，并只能出价一次，安装成就值高低排序
    */
    _evt_grab() {
        let that = this;
        let userInfo = globalData.user;
        let demandInfo = globalData.current_demand;
        let coin = 0;
        if (userInfo.user_type == 100) {
            if (userInfo.avatar) {
                /**弹出即将支付多少汇币 */
                coin = parseInt($('.offer-input').val()) || 0;
                if (demandInfo.status == '8') {
                    coin = demandInfo.expected_bid_point;
                }
                if (demandInfo.status == '20') {
                    coin = demandInfo.price;
                }
                if (demandInfo.status == '30') {
                    coin = 0;
                }
                if (coin >= 200 || that.isBomb) {
                    coin = 200;
                }
                funs.openModal('modules/modals/CommonModal.JS', function (mod) {
                    mod.setType(20, { coin: coin });
                })

            } else {
                //弹出完善个人资料信息弹窗
                funs.openModal('modules/modals/CommonModal.JS', function (mod) {
                    mod.setType(2);
                })
            }
        } else {
            // 游客，去登陆
            if (globalData.user.user_type == 0) {
                funs.openModal('modules/modals/CommonModal.JS', function (mod) {
                    mod.setType(1);
                })

                // 客户,去完善资料
            } else if (globalData.user.user_type == 1) {
                funs.openModal('modules/modals/CommonModal.JS', function (mod) {
                    mod.setType(2);
                })
            }
        }
    }

    /*拨打电话方法*/
    _evt_fromWPH() {
        funs.openModal('modules/modals/CommonModal.js', function (mod) {
            mod.setType(6, { phone: globalData.current_demand.user.phone })
        })
    }


    // 页面卸载事件
    afterUninstall() {
        let that = this;
        // 如果录音存在，离开洽谈室时重置录音播放；
        if (globalData.current_demand.audio) {
            //播放完成
            let audio = document.getElementsByTagName('audio')[0];
            audio.currentTime = 0;
            audio.pause();
            this.find('.play-pause>span').removeClass('icon-pause-one').addClass('icon-play-one');
            this.find('.audio-time').addClass('hide');
            this.find('.txt').removeClass('hide');
        }
        //倒计时清空
        window.clearInterval(that.int1);
        window.clearInterval(that.int2);
        this.find('.count-deadline').text('');
        globalData.currPage = 'index';
    }

    //计算竞价起拍价格
    countOfferPoint() {
        let demand = globalData.current_demand;
        let list = demand.supplier_list;
        let start_point = demand.bid_start_point;
        let realPoint;
        //首轮竞价
        if (demand.status == '10') {

            // 如果参与过
            if (demand.supplier_partic_status != '0') {
                if (demand.my_offer >= 200) {
                    demand.self_has_max = true;
                } else {
                    demand.self_has_max = false;
                }
                realPoint = demand.my_offer;
            } else {
                realPoint = start_point;
            }

        }
        //已经抢单结束或项目结束
        if (parseInt(demand.status) >= 100) {
            realPoint = start_point;
        }
        // 二次竞价中
        if (demand.status == '20') {
            realPoint = demand.price;
        }
        //免费竞价状态
        if (demand.status == '30') {
            realPoint = '0';
        }
        if (realPoint >= 200) {
            realPoint = 200;
        }
        this.pointAtLeast = realPoint;
        if ($('.new-offer-input').val() && (parseInt($('.new-offer-input').val().trim()) >= realPoint)) { return; }
        $('.new-offer-input').val(realPoint);
        demand.curr_offer = realPoint;

    }


    //竞价项目倒计时
    grabCount() {
        let demand = globalData.current_demand;
        if (demand.cate == '4') return;
        let dl;
        // 进行中的项目或铜牌一口价，取项目的发布时间
        if (demand.status == '10' || demand.status == '8') {
            dl = demand.published_at;
            if (!dl) return;
            dl = dl * 1000;
            window.clearInterval(this.int1);
            this.int1 = setInterval(() => {
                let now = new Date().getTime();
                let m, s;
                //获得跟当前时间的间隔秒数
                let gapSec = Math.floor((now - dl) / 1000);
                //如果超过5分钟
                if (gapSec > 60 * 5) {
                    window.clearInterval(this.int1);
                    $('.deadline').hide();
                    return
                }
                gapSec = 60 * 5 - gapSec;
                m = parseInt(gapSec / 60);
                if (m > 0) {
                    s = gapSec - 60 * m;
                } else {
                    s = gapSec;
                }
                if (m < 10) m = '0' + m;
                if (s < 10) s = '0' + s;

                let text = '00:' + m + ':' + s;
                $('.deadline').show();
                this.find('.count-deadline').text(text);
            }, 1000);
            //竞价成功的项目，取竞价成功的时间
        } else if (demand.supplier_partic_status == '110' || demand.supplier_partic_status == '120') {
            let list = demand.supplier_list;
            $.each(list, function (index, value) {
                if (value.id == globalData.user.user_id) {
                    dl = value.bid_win_at;
                }
            })
            if (!dl) return;
            dl = dl * 1000;
            window.clearInterval(this.int3);
            this.int3 = setInterval(() => {
                let now = new Date().getTime();
                let m, s;
                //获得跟当前时间的间隔秒数
                let gapSec = Math.floor((now - dl) / 1000);
                //如果超过5分钟
                if (gapSec > 60 * 5) {
                    window.clearInterval(this.int3);
                    $('.deadline').hide();
                    return
                }
                gapSec = 60 * 5 - gapSec;
                m = parseInt(gapSec / 60);
                if (m > 0) {
                    s = gapSec - 60 * m;
                } else {
                    s = gapSec;
                }
                if (m < 10) m = '0' + m;
                if (s < 10) s = '0' + s;

                let text = '00:' + m + ':' + s;
                $('.deadline').show();
                this.find('.count-deadline').text(text);
            }, 1000);
            // $('.deadline').hide();
        }

    }

    /** 点击竞价规则滚动到页面底部 */
    _evt_scrollToRules() {
        $('#collapseFour').collapse('show');
        $('#DemandDetail').animate({ scrollTop: $('#DemandDetail')[0].scrollHeight }, 600);
    }

    /** 爆灯 */
    _evt_bomb() {
        if (globalData.current_demand.tag == 2) {
            //TO2
            return;
        }
        if (globalData.current_demand.tag == 1) {
            funs.openModal('modules/modals/CommonModal.js', function (mod) {
                mod.setType(5);
            });
            return;
        }
        //查看余额是否充足
        if (parseInt(globalData.pack.bid_point) >= 200) {
            //抢单
            api.bidDemand({ "id": globalData.current_demand.id, offer: '200' }, {
                succ: function (json) {
                    // 更新项目信息
                    let curr_pro = globalData.current_demand;
                    curr_pro.supplier_partic_status = json.demand_info.supplier_partic_status;
                    curr_pro.bid_cnt = json.demand_info.demand.bid_cnt;
                    curr_pro.status = json.demand_info.demand.status;
                    curr_pro.user.phone = json.demand_info.user.mobile;
                    curr_pro.audio = json.demand_info.demand.audio;
                    curr_pro.my_offer = parseInt(json.demand_info.my_offer);
                    if (curr_pro.my_offer >= 200) {
                        curr_pro.self_has_max = true;
                    } else {
                        curr_pro.self_has_max = false;
                    }
                    if (json.demand_info.supplier_partic_status == 10 && json.demand_info.demand.status == 10) {
                        //首次出价
                        funs.tip('出价成功', '请保持领先状态，以免出局', 'succ');
                    } else if (json.demand_info.supplier_partic_status == 10 && (json.demand_info.demand.status == 20 || json.demand_info.demand.status == 30)) {
                        //二次进价时，或免费竞价中
                        funs.tip("出价成功", "若领先者放弃联系客户，您将有机会获得客户信息", "succ");
                    } else if (json.demand_info.supplier_partic_status == 100 || json.demand_info.supplier_partic_status == 120) {
                        //竞价成功弹窗
                        // tomato.getVPresenter<GrabStatusModal>(funs.moduleToUrl('m/grab/GrabStatusModal'), (GrabStatusModal) => {
                        //     GrabStatusModal.grabSucc();
                        //     tdom.open(GrabStatusModal, tdom.DialogTarget.Blank, {
                        //         size: { width: '90%', height: tomato.DialogSize.Content },
                        //         effect: tdom.DialogEffect.scale,
                        //         masked: true
                        //     });
                        // })
                        // 显示电话是虚拟号的提示信息
                        // tomato.getVPresenter<demand>(funs.moduleToUrl('m/DemandDetail'), (Demand) => {
                        //     setTimeout(function () {
                        //         Demand.showAgreementTip();
                        //     }, 3200)
                        // });
                    }

                    // 更新剩余次数
                    api.getMyPack({
                        succ: function (json) {
                            globalData.pack.bid_point = json.pack.bid_point;
                        }
                    });
                    // 获取项目参与列表
                    api.getParticInfo({ demand_id: globalData.current_demand.id }, {
                        succ: function (json) {
                            let list = json.partic_supplier_list;
                            let bronze_partic_info = json.bronze_partic_info;
                            //铜牌一口价下成功信息
                            $.extend(true, globalData.current_demand.bronze_partic_info, bronze_partic_info);
                            globalData.current_demand.supplier_list.length = 0;
                            globalData.current_demand.supplier_list.push(...list);
                        }
                    });
                },
                fail: function (data) {

                    if ((data.status == '41028002') || (data.status == '41028003') || (data.status == '41028022')) {
                        //额度不足弹窗
                        funs.openModal('modules/modals/CommonModal.js', function (mod) {
                            mod.setType(11);
                        });
                    } else {
                        funs.tip("竞价失败", data.msg, 'fail');
                    }
                }
            })
        } else {
            ///额度不足弹窗
            funs.openModal('modules/modals/CommonModal.js', function (mod) {
                mod.setType(11);
            });
        }
    }

    /** 反馈信息 */
    _evt_openFeedbackDemand() {
        let pro_info = globalData.current_demand;
        if (pro_info) {
            location.hash = 'project/FeedbackDemand?did=' + pro_info.id;
        }
    }

    /** 申报结束 */
    _evt_loadDeclareProject() {
        location.hash = 'project/DeclareProject';
    }

    _evt_goIndex() {
        location.hash = 'index';
    }

    /*================一次竞价抢单逻辑－start===============
    * 当出价高于200时，最高为200，并只能出价一次，按照成就值高低排序
    */
    _evt_NewGrab() {
        if (!globalData.preventRepeatedlyClick) {
            return
        }
        globalData.preventRepeatedlyClick = false;
        let that = this;
        let userInfo = globalData.user;
        let demandInfo = globalData.current_demand;
        if (userInfo.user_type == 100) {
            if (userInfo.avatar) {
                /**弹出新的报价弹窗 */
                that._showGrabModal()

            } else {
                //弹出完善个人资料信息弹窗
                //弹出完善个人资料信息弹窗
                funs.openModal('modules/user/UpdateSupplierInfo.js', null);
            }
        } else {
            // 如果是客户，去完善资料成为工程商
            if (globalData.user.user_type == 1) {
                funs.openModal('modules/modals/CommonModal.js', function (mod) {
                    mod.setType(2);
                });
                // 游客，去登陆
            } else if (globalData.user.user_type == 0) {
                funs.openModal('modules/modals/CommonModal.js', function (mod) {
                    mod.setType(1);
                });
            }

        }
        globalData.preventRepeatedlyClick = true;
    }

    _evt_bomb2() {
        this._evt_bomb();
        this.hideGrabModal();
    }
    _evt_bidDemand() {
        let that = this;
        that.hideGrabModal();
        let data = globalData;
        let demand_id = globalData.current_demand.id;
        let bid_point = $('.new-offer-input').val();
        if (!bid_point || bid_point == 0) { return };
        if (globalData.current_demand.tag == 2) {
            //TO2
            // tomato.getVPresenter<tomato.VPresenter>(funs.moduleToUrl('m/grab/GrabFailModal'), (GrabFailModal) => {
            //     tdom.open(GrabFailModal, tdom.DialogTarget.Blank, {
            //         size: { width: '90%', height: tomato.DialogSize.Content },
            //         effect: tdom.DialogEffect.scale,
            //         masked: true
            //     });
            // });
            return;
        }
        if (globalData.current_demand.tag == 1) {
            //TO1
            funs.openModal('modules/modals/CommonModal.js', function (mod) {
                mod.setType(5);
            });
            return;
        }
        //判断套餐是否过期
        if (parseInt(data.pack.bid_point) >= parseInt(bid_point)) {
            //抢单
            api.bidDemand({ "id": demand_id, offer: bid_point }, {
                succ: function (json) {
                    // 更新项目信息
                    let curr_pro = globalData.current_demand;
                    curr_pro.supplier_partic_status = json.demand_info.supplier_partic_status;
                    curr_pro.bid_cnt = json.demand_info.demand.bid_cnt;
                    curr_pro.status = json.demand_info.demand.status;
                    curr_pro.user.phone = json.demand_info.user.mobile;
                    curr_pro.audio = json.demand_info.demand.audio;
                    curr_pro.my_offer = parseInt(json.demand_info.my_offer);
                    if (curr_pro.my_offer >= 200) {
                        curr_pro.self_has_max = true;
                    } else {
                        curr_pro.self_has_max = false;
                    }
                    let pro_list = globalData.project_list;
                    // 更新项目列表中此项目的信息
                    for (let i = 0; i < pro_list.length; i++) {
                        if (curr_pro.id == pro_list[i].id) {
                            pro_list[i] = curr_pro;
                            break;
                        }
                    }
                    // 更新参与项目列表信息
                    let joined_list = globalData.joined_list;
                    let hasPro = false;
                    for (let i = 0; i < joined_list.length; i++) {
                        if (curr_pro.id == joined_list[i]) {
                            joined_list[i] = $.extend(joined_list[i], curr_pro);
                            hasPro = true;
                        }
                    }
                    if (!hasPro) {
                        joined_list.push(curr_pro);
                    }
                    if (json.demand_info.supplier_partic_status == 10 && json.demand_info.demand.status == 10) {
                        //首次出价
                        funs.tip('出价成功', '请保持领先状态，以免出局', 'succ');
                    } else if (json.demand_info.supplier_partic_status == 10 && (json.demand_info.demand.status == 20 || json.demand_info.demand.status == 30)) {
                        //二次进价时，或免费竞价中
                        funs.tip("出价成功", "若领先者放弃联系客户，您将有机会获得客户信息", "succ");
                    } else if (json.demand_info.supplier_partic_status == 100 || json.demand_info.supplier_partic_status == 120) {
                        //竞价成功弹窗

                        // 显示电话是虚拟号的提示信息
                    }

                    // 更新剩余次数
                    api.getMyPack({
                        succ: function (json) {
                            globalData.pack.bid_point = json.pack.bid_point;
                        }
                    });
                    // 获取项目参与列表
                    api.getParticInfo({ demand_id: globalData.current_demand.id }, {
                        succ: function (json) {
                            let list = json.partic_supplier_list;
                            let bronze_partic_info = json.bronze_partic_info;
                            //铜牌一口价下成功信息
                            $.extend(true, globalData.current_demand.bronze_partic_info, bronze_partic_info);
                            globalData.current_demand.supplier_list.length = 0;
                            globalData.current_demand.supplier_list.push(...list);
                        }
                    });
                },
                fail: function (data) {
                    if ((data.status == '41028002') || (data.status == '41028003') || (data.status == '41028022')) {
                        //额度不足弹窗
                        funs.openModal('modules/modals/CommonModal.js', function (mod) {
                            mod.setType(11);
                        });
                    } else {
                        funs.tip("竞价失败", data.msg, 'fail');
                    }
                }
            });
        } else {
            ///额度不足弹窗
            funs.openModal('modules/modals/CommonModal.js', function (mod) {
                mod.setType(11);
            });
        }
    }

    // 隐藏出价框
    _evt_hideGrabModal(obj, hit) {
        // if (obj === hit) {
        $('#NewGrabModal').find('.content').css({ "transform": "scale(0,0)", "opacity": "0" });
        setTimeout(function () {
            $('#NewGrabModal').hide();
        }, 300)
        // }
    }
    hideGrabModal() {
        $('#NewGrabModal').find('.content').css({ "transform": "scale(0,0)", "opacity": "0" });
        setTimeout(function () {
            $('#NewGrabModal').hide();
        }, 300)
    }

    _showGrabModal() {
        $('#NewGrabModal').show().find('.content').css({ "transform": "scale(1,1)", "opacity": "1" });
        $('.new-offer-input').focus();

        let height = $(window).height();
        let inputHeight = $('#NewGrabModal .input-content').height();
        let titleHeight = $('#NewGrabModal .partical-suppliers h3').height();
        $('#NewGrabModal .partical-suppliers').height(height - inputHeight);
        $('#NewGrabModal .partical-suppliers .list').height(height - inputHeight - titleHeight - 30);


    }

    _evt_loadRefunds() {
        location.hash = 'project/demand/Refunds';
    }
    _evt_loadValueAddService() {
        location.hash = 'pack/MyPack';
    }

    /*正在呼叫提示*/
    _evt_garbCalling() {
        funs.openModal('modules/modals/CommonModal.js', function (mod) {
            mod.setType(15);
        })
    }

    /*出价框加减*/
    _evt_computed(obj, hit) {
        let type = $(hit).attr('type');
        let val = parseInt($('.new-offer-input').val().trim());
        if ('add' == type) {
            val++;
        } else {
            if (this.pointAtLeast >= val) {
                return;
            }
            val--;
        }
        $('.new-offer-input').val(val);
        globalData.current_demand.curr_offer = val;
    }


    // 调用app聊天功能
    _evt_goChat(){
        AppUtil.jsCallAppParam("20", {});
    }
}

